package com.itheima.server.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.server.domain.Business;
import com.itheima.server.domain.BusinessDetail;
import com.itheima.server.domain.CluBusSave;
import com.itheima.server.service.BusinessDetailService;
import com.itheima.server.service.IBusinessService;
import com.itheima.server.service.RuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class BusinessJob {
    @Autowired
    private RuleService ruleService;
    @Autowired
    private BusinessDetailService businessDetailService;
    @Autowired
    private IBusinessService businessService;

    public void executeBusiness() {
        log.info("商机定时任务开始");
        //逻辑：最后更新时间+限制时间>=当前时间触发回收
        //获取定时任务参数
        LambdaQueryWrapper<CluBusSave> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CluBusSave::getType, 1);
        CluBusSave busSave = ruleService.getOne(wrapper);
        Integer limitTime = busSave.getLimitTime();
        String limitTimeType = busSave.getLimitTimeType();



            //获取遍历商机详情

            List<BusinessDetail> list = businessDetailService.list();

            for (BusinessDetail detail : list) {

                //判断商机详情最后更新时间是否为空
                if (detail.getUpdateTime() == null) {
                    continue;
                }

                Business business = businessService.getOne(Wrappers.<Business>lambdaQuery().eq(Business::getId, detail.getBusinessId()));
                //判断是不是已经转化为合同
                if (business.getStatus().equals(3)){
                    continue;
                }


                //判断是否在公海
                if (business.getSea().equals(1)) {

                    continue;
                }
                //判断商机更新时间为空
                if (business.getUpdateTime() == null) {
                    continue;
                }


                //小时
                if (limitTimeType.equals("0")) {
                    if (detail.getUpdateTime().getTime() + limitTime * 60 * 60 * 1000 <= System.currentTimeMillis()||
                            business.getUpdateTime().getTime() + limitTime* 60 * 60 * 1000 <= System.currentTimeMillis()) {
                        updateStatus(detail, business);
                        continue;
                    }
                }

                //天
                if (limitTimeType.equals("1")) {
                    if (detail.getUpdateTime().getTime() + limitTime* 24 * 60 * 60 * 1000 <= System.currentTimeMillis()||
                            business.getUpdateTime().getTime() + limitTime*24 * 60 * 60 * 1000 <= System.currentTimeMillis()) {
                        updateStatus(detail, business);
                        continue;
                    }
                }

                //周
                if (limitTimeType.equals("2")) {
                    if (detail.getUpdateTime().getTime() + limitTime *7 * 24 * 60 * 60 * 1000 <= System.currentTimeMillis()||
                            business.getUpdateTime().getTime() + limitTime*7 * 24 * 60 * 60 * 1000 <= System.currentTimeMillis()) {
                        updateStatus(detail, business);
                        continue;
                    }
                }




            }
            log.info("商机定时任务结束");




    }

    //修改状态方法
    private void updateStatus(BusinessDetail detail, Business business) {
        //修改商机
        business.setSea(1);
        businessService.updateById(business);
        //修改商机详情
        detail.setUpdateTime(new Date());
        businessDetailService.updateById(detail);
        log.info("商机回收成功，商机id:{}", detail.getBusinessId());
    }

}
